package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Product;
import com.example.exception.FloraException;
import com.example.mapper.ProductMapper;
import com.example.service.ProductCategoryService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductMapper productMapper;

    /*@Override
    public ResponseResult getAllProductList() {
        LambdaQueryWrapper<Product> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(Product::getStatus, SystemConstants.PRODUCT_STATUS_ON_SHELF);
        List<Product> products = list(productWrapper);

        List<ProductVo> productVos = BeanCopyUtils.copyBeanList(products, ProductVo.class);
        return ResponseResult.okResult(productVos);
    }*/

    @Override
    public IPage<Product> getProductPage(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        Page<Product> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Product> qw = new QueryWrapper();
        //上架的商品才能看到
        qw.eq("status",0);

        if(condition.containsKey("title")){
            qw.like("title",condition.get("title"));
        }
        if(condition.containsKey("price")){
            qw.lt("price",condition.get("price"));
        }
        if(condition.containsKey("created_user_id")){
            qw.eq("created_user_id",condition.get("created_user_id"));
        }
        if(condition.containsKey("category_id")) {
            qw.eq("category_id", condition.get("category_id"));
        }

        Page<Product> page1 = (Page<Product>) this.baseMapper.getProductPage(page,qw);
        System.out.println(page1.getRecords());
        return page1;
    }


    @Override
    @Transactional
    public Product add(Product product) throws FloraException{
        if(judgeDataValidity(product)){
            try {
                this.baseMapper.insert(product);
                return product;
            } catch (Exception e) {
                throw new FloraException("添加文章失败");
            }
        }
        return null;
    }

    public Boolean judgeDataValidity(Product product) throws FloraException {
        if(productCategoryService.getById(product.getCategoryId())==null|| productCategoryService.getById(product.getCategoryId()).getStatus()==1){
            throw new FloraException("商品分类不存在");
        }
        return true;
    }

    @Override
    public void update(Product product) throws FloraException {
        //先判断要修改的商品是否存在
        if(this.baseMapper.selectById(product.getId())==null){
            throw new FloraException("要修改的商品不存在");
        }

        if(judgeDataValidity(product)) {
            try {
                this.baseMapper.updateById(product);
            } catch (Exception e) {
                throw new FloraException("修改商品失败" + e.getMessage());
            }
        }
    }

    @Override
    @Transactional
    public void delete(Long id) throws FloraException {
        Product product = this.baseMapper.selectById(id);
        if(product==null){
            throw new FloraException("要删除的商品不存在");
        }
        try{
            this.baseMapper.deleteById(id);
        } catch (Exception e) {
            throw new FloraException("删除商品失败"+e.getMessage());
        }
    }
}

